<?php

namespace App\Http\Controllers;

use App\Exceptions\PhonicsBonusBalanceIsZeroException;
use App\Exceptions\PhonicsStudentNotFoundException;
use App\Facades\Consumer;
use App\Http\Response\CreatedResponse;
use App\Models\PhonicsShare;
use App\Models\PhonicsStudent;
use App\Models\PhonicsWithdraw;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class PhonicsWithdrawController extends Controller
{

    public function retrieveSummary(Request $request)
    {
        $studentId = Consumer::getUserId();

        /**
         * @var PhonicsStudent $phonicsStudent
         */
        $phonicsStudent = PhonicsStudent::find($studentId);
        if (empty($phonicsStudent)) {
            throw new PhonicsStudentNotFoundException();
        }

        $records = [];
        $shares = PhonicsShare::where('student_id', $studentId)->get();
        if (!empty($shares)) {
            foreach ($shares as $share) {
                $records[] = [
                    'id' => $share->getId(),
                    'bonus' => $share->getBonus(),
                ];
            }
        }

        return new JsonResponse([
            'privilege' => $phonicsStudent->getSharePrivilege(),
            'bonus' => floatval($phonicsStudent->getBonus()),
            'bonusWithdraw' => floatval($phonicsStudent->getBonusWithdraw()),
            'bonusLeft' => floatval($phonicsStudent->getBonusLeft()),
            'shareCount' => intval($phonicsStudent->getShareCount()),
            'records' => $records,
        ]);
    }

    public function retrieveHistory(Request $request)
    {
        $studentId = Consumer::getUserId();

        /**
         * @var PhonicsStudent $phonicsStudent
         */
        $phonicsStudent = PhonicsStudent::find($studentId);
        if (empty($phonicsStudent)) {
            throw new PhonicsStudentNotFoundException();
        }

        $withdraws = PhonicsWithdraw::where('student_id', $studentId)->get();

        $result = [];
        /**
         * @var PhonicsWithdraw $withdraw
         */
        foreach ($withdraws as $withdraw) {
            $result[] = [
                'amount' => floatval($withdraw->getAmount()),
                'status' => $withdraw->getStatus(),
                'errCode' => $withdraw->getErrorCode(),
                'errMessage' => $withdraw->getErrorMessage()
            ];
        }

        return new JsonResponse($result);
    }

    public function create(Request $request)
    {
        $this->validate($request, [
            'openId' => 'required|string'
        ]);
        $studentId = Consumer::getUserId();

        /**
         * @var PhonicsStudent $phonicsStudent
         */
        $phonicsStudent = PhonicsStudent::find($studentId);
        if (empty($phonicsStudent)) {
            throw new PhonicsStudentNotFoundException();
        }

        $bonusLeft = $phonicsStudent->getBonusLeft();
        if ($bonusLeft <= 0) {
            throw new PhonicsBonusBalanceIsZeroException();
        }

        /**
         * @var PhonicsWithdraw $phonicsWithdraw
         */
        $phonicsWithdraw = new PhonicsWithdraw();
        $phonicsWithdraw->setStudentId($studentId);
        $phonicsWithdraw->setOpenId($request->input('openId'));
        $phonicsWithdraw->setAmount($bonusLeft);
        $phonicsWithdraw->setStatus(PhonicsWithdraw::STATUS_CREATED);
        $phonicsWithdraw->save();

        $phonicsStudent->setBonusWithdraw($bonusLeft + $phonicsStudent->getBonusWithdraw());
        $phonicsStudent->setBonusLeft(0);
        $phonicsStudent->save();

        return new CreatedResponse();
    }
}
